import 'dart:async';

import 'package:chathouse/public/Config.dart';
import 'package:flutter/material.dart';

import '../../../public/event_bus.dart';
import '../../../util/base_utils.dart';
import '../../../widgets/base_appbar.dart';
import '../../../widgets/buttons_tabbar.dart';
import 'client_order_list.dart';

class ClientOrderPage extends StatefulWidget {
  const ClientOrderPage({Key? key}) : super(key: key);

  @override
  _ServiceDemandPageState createState() => _ServiceDemandPageState();
}

class _ServiceDemandPageState extends State<ClientOrderPage>
    with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  @override
  bool get wantKeepAlive => true;

  late TabController tabController;
  StreamSubscription? refreshSubscription;

  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 3, vsync: this);
    refreshSubscription = eventBus.on<UpdateClientOrderEvent>().listen((event) {
      if (!mounted) return;
      tabController.animateTo(event.idx as int);
    });
  }

  @override
  void dispose() {
    tabController.dispose();
    refreshSubscription?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: getAppBar(leading: Container(), title: "訂單"),
        body: Column(
          children: <Widget>[
            ButtonsTabBar(
              controller: tabController,
              duration: 0,
              labelStyle: getTextStyle(16,
                  color: MyColors.red_2166474, fontType: FontType.BOLD),
              unselectedLabelStyle: getTextStyle(16,
                  color: MyColors.black_0000011, fontType: FontType.BOLD),
              contentPadding: EdgeInsets.symmetric(horizontal: 40.w),
              buttonMargin:
                  EdgeInsets.only(bottom: 0, left: 2.w, right: 2.w, top: 12.w),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(14.w),
                      topRight: Radius.circular(14.w))),
              unselectedDecoration: BoxDecoration(
                  color: MyColors.gary_210210210,
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(14.w),
                      topRight: Radius.circular(14.w))),
              tabs: const [
                Tab(
                  text: "全部",
                ),
                Tab(
                  text: "進行中",
                ),
                Tab(
                  text: "我的評語",
                ),
              ],
            ).setContainer(
              alignment: Alignment.center,
              height: 45.w,
              width: double.infinity,
              color: MyColors.gary_232233237,
              // padding: EdgeInsets.only(left: 18.w, right: 18.w)
            ),
            Expanded(
              child: TabBarView(
                controller: tabController,
                children: <Widget>[
                  ClientOrderList(null),
                  ClientOrderList(1),
                  ClientOrderList(2),
                ],
              ).setContainer(
                  margin: EdgeInsets.only(left: 16, right: 16, top: 6.w)),
            ),
          ],
        ));
  }
}
